package cn.wildfire.chat.kit.user;

import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

import com.sourcemuch.im.caiba.R;

import cn.wildfire.chat.app.extendsion.utils.TextUtils;
import cn.wildfire.chat.app.main.UserRemarkMoreActivity;
import cn.wildfire.chat.kit.WfcBaseActivity;
import cn.wildfire.chat.kit.WfcUIKit;
import cn.wildfire.chat.kit.contact.ContactViewModel;
import cn.wildfirechat.model.GroupInfo;
import cn.wildfirechat.model.UserInfo;
import cn.wildfirechat.remote.ChatManager;

public class UserInfoActivity extends WfcBaseActivity {

    private UserInfo userInfo;
    private GroupInfo groupInfo;
    private int privateChat; // 群员相互交友标志
    private String normalUser; // 普通群员
    private String fromSelfCenter; // 来自于个人中心点击
    UserInfoFragment userInfoFragment;
    PersonalInfoFragment personalInfoFragment;
    private UserViewModel userViewModel;

    private String myUid;
    private boolean fromGroupShow;
    private boolean commonGroupLayoutShow;

    public UserInfoFragment getUserInfoFragment() {
        return userInfoFragment;
    }

    @Override
    protected int contentLayout() {
        return R.layout.fragment_container_activity;
    }

    @Override
    protected void afterViews() {
        /**
         * modify by HEKE , 2019/8/23 18:48
         * TODO 修改交友设定
         */
        Intent intent = getIntent();
        userInfo = intent.getParcelableExtra("userInfo");
        groupInfo = intent.getParcelableExtra("groupInfo");
        myUid = intent.getStringExtra("MyUID");
        privateChat = intent.getIntExtra("privateChat", -1);
        normalUser = intent.getStringExtra("normal");
        fromSelfCenter = intent.getStringExtra("fromSelfCenter");
        fromGroupShow = intent.getBooleanExtra("fromGroupShow", false);
        commonGroupLayoutShow = intent.getBooleanExtra("commonGroupLayoutShow", true);
        if (userInfo == null) {
            finish();
        } else {
            userViewModel = WfcUIKit.getAppScopeViewModel(UserViewModel.class);
            /**
             * 来自于个人中心的查看
             * */
            if (!TextUtils.isTextEmpty(fromSelfCenter) && fromSelfCenter.equals("true")) {
                personalInfoFragment = PersonalInfoFragment.newInstance(userInfo);
                getSupportFragmentManager().beginTransaction()
                        .replace(R.id.containerFrameLayout, personalInfoFragment)
                        .commit();
                if (personalInfoFragment != null && !TextUtils.isTextEmpty(normalUser) && privateChat != -1) {
                    personalInfoFragment.setPrivateChatAttrs(privateChat, normalUser.equals("0"));
                }
            }
            /**
             * 其他统一入口
             * */
            else {
                userInfoFragment = UserInfoFragment.newInstance(userInfo, groupInfo, myUid, fromGroupShow, commonGroupLayoutShow);
                getSupportFragmentManager().beginTransaction()
                        .replace(R.id.containerFrameLayout, userInfoFragment)
                        .commit();
                if (userInfoFragment != null && !TextUtils.isTextEmpty(normalUser) && privateChat != -1) {
                    userInfoFragment.setPrivateChatAttrs(privateChat, normalUser.equals("0"));
                }
            }

        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        boolean isSelf = userInfo.uid.equals(ChatManager.Instance().getUserId());
        setToolbarOrReplaceType(true, 2);
        setReplaceTitle(null, isSelf ? R.string.modify_personal_info : R.string.user_info);
        if (isSelf) {
            setReplaceTitle(null, R.string.personal_info);
        }
        setSearchLayoutVisibility(false);
        try {
            boolean moreShow = (!isSelf && (ChatManager.Instance().isMyFriend(userInfo.uid)||ChatManager.Instance().isMyBlackList(userInfo.uid)));
            if (moreShow) {
                setMoreButtonThenBindClickForRightButtonView(View.VISIBLE, R.mipmap.icon_more, new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Intent intent = new Intent(UserInfoActivity.this, FriendDataSettingActivity.class);
                        intent.putExtra("userInfo", userInfo);
                        startActivity(intent);
                    }
                });
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected int menu() {
        return R.menu.user_info;
    }

    @Override
    protected void afterMenus(Menu menu) {
        super.afterMenus(menu);
        ContactViewModel contactViewModel = WfcUIKit.getAppScopeViewModel(ContactViewModel.class);
        if (!contactViewModel.isFriend(userInfo.uid)) {
            MenuItem item = menu.findItem(R.id.more);
            item.setEnabled(false);
            item.setVisible(false);
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == R.id.more) {
            Intent intent = new Intent(this, UserRemarkMoreActivity.class);
            intent.putExtra("userId", userInfo.uid);
            startActivity(intent);
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    protected void setTitleCenterThenSetSearchView() {
        super.setTitleCenterThenSetSearchView();
        setToolbarOrReplaceType(true, 3);
        setReplaceTitle(getTitle().toString(), -1);
        setSearchLayoutVisibility(false);
    }
}
